var curNode;
var orgTree;
var firstNode;
var orgStaffTable;
var menuLength = getLength("config_page_size");
// 组织树设置
var orgSetting = {
	view : {
		selectedMulti : false
	},
	data : {
		simpleData : {
			enable : true,
			idKey : "orgId",
			pIdKey : "parentId"
		},
		key : {
			name : "orgName"
		}
	},
	callback : {
		beforeClick : function(treeId, treeNode) {
			curNode = treeNode;
			showOrgDetails(curNode.orgId);
		}
	},
	async : {
		enable : true,
		url : "../gacxOrgController/getChildrenOrgsById",
		autoParam : [ "orgId" ]
	}
};
var checkValidator = {
	message : 'This value is not valid',
	feedbackIcons : {
		valid : 'glyphicon glyphicon-ok',
		invalid : 'glyphicon glyphicon-remove',
		validating : 'glyphicon glyphicon-refresh'
	},
	fields : {
		orgName : {
			validators : {
				notEmpty : {
					message : '请输入组织名称'
				},
				stringLength : {
					max : 100,
					message : '组织名称不大于100个字符'
				}
			}
		}
	},
	submitHandler : function(validator, form, submitButton) {
		// Use Ajax to submit form data
		if ($(submitButton).attr('id') == 'addOrg') {
			addOrg();
		}
		if ($(submitButton).attr('id') == 'updateOrg') {
			updateOrg();
		}
	}
};
$(function() {
	// 初始化树
	$("#tree").height($(window).height() - 120);
	orgTree = $.fn.zTree.init($("#tree"), orgSetting, zNodes);
	firstNode = $("#firstNode").val();
	curNode = orgTree.getNodeByParam("orgId", firstNode);
	orgTree.selectNode(curNode, false, false);
	showOrgDetails(curNode.orgId);
	$('#orgObj').bootstrapValidator(checkValidator);
	$("#parentDisId").change(function(){
		var parentDisId = $(this).val();
		var parentId = $("#"+parentDisId).attr("data_id");
		getSecondDis(parentDisId,null);
});
});
function getSecondDis(parentId,childId){
	$.post("getSecondDis.action", {
			"disId" : parentId
		}, function(data) {
			if (data) {
				$('#disId').append(
						"<option value=\"\">" + "-------请选择-------"+ "</option>");
				for (var i = 0; i < data.length; i++) {
					$('#disId').append(
							"<option data_id= \"" + data[i].disId+ "\" value=\"" + data[i].disId+ "\">" + data[i].disName+ "</option>");
				}
				if (childId) {
					document.getElementById("disId").value = childId;
				}
			}
		})
		
}
function resetValidator() {
	if ($('#orgObj').data('bootstrapValidator')) {
		$('#orgObj').data('bootstrapValidator').resetForm(false);
	}
}
// 展开所有
function expandAll() {
	orgTree.expandAll(true);
}

// 关闭所有
function collapseAll() {
	orgTree.expandAll(false);
}
function initFrame() {
	$('#orgObj input').val("");
	$('#orgShow input').val("");
	$('#menuInfoWell').hide();
	$('#orgShow').show();
	$('#pdisName1').attr('display:none;');
	$('#disName1').attr('display:none;');
	resetValidator();
}
function showAdd() {
	if (!curNode) {
		alertModel('请选择父节点');
		return;
	}
	$('#parentDisId').empty();
	$('#disId').empty();
	$.post("../gacxOrgController/getFirstDis.action", {}, function(data) {
		if (data) {
			$('#parentDisId').append(
					"<option value=\"\">" + "-------请选择-------" + "</option>");
			for (var i = 0; i < data.length; i++) {
				$('#parentDisId').append(
						"<option value=\"" + data[i].disId + "\">"
								+ data[i].disName + "</option>");
			}
		}
	});
	$('#orgObj input').val("");
	$('#orgShow').hide();
	$('#menuInfoWell').show();
	$('#addOrg').show();
	$('#updateOrg').hide();
	var orgKind = $('#parentOrgType').val();
	$('#orgKind').val(orgKind);
	if (1 == orgKind) {
		$('#orgKindShow').html("联通");
	} else {
		$('#orgKindShow').html("公安");
	}
	$('#parentId').val(curNode.orgId);
	$('#parentName').html(curNode.orgName);
	$('#addOrg').html('保存');
	if (null == $('#orgObj').data('bootstrapValidator')) {
		$('#orgObj').bootstrapValidator(checkValidator);
	}
}
function addOrg() {
	var parentDisId = $('#parentDisId').val();
	var disId = $('#disId').val();
	if (!parentDisId && !disId) {
		alertModel("请选择所属省市");
		return;
	}
	$.post('addOrg.action', $('#orgObj').serialize(), function(data) {
		alert("添加成功！");
		initFrame();
		var newNode = {
			'orgId' : data.orgId,
			'orgName' : data.orgName,
			'parentId' : curNode.orgId
		};
		var curNode1 = orgTree.addNodes(curNode, newNode);
		curNode = curNode1[0];
		showData(data);
		resetValidator();
	});
}
function delOrgConfirm() {
	if (!curNode) {
		alertModel('请选择需删除的节点');
		return;
	}
	if (1 == curNode.orgId) {
		alertModel('根组织禁止删除');
		return;
	}
	confirmModel('确定删除组织' + curNode.orgName + '及其子节点么？', delOrg, "")
}
function delOrg() {
	var treeObj = $.fn.zTree.getZTreeObj("tree");
	var treeNode = treeObj.getSelectedNodes();
	var childNodes = treeObj.transformToArray(treeNode);
	var nodes = new Array();
	for (var i = 0; i < childNodes.length; i++) {
		nodes.push(childNodes[i].orgId);
	}
	var orgIds = nodes.join(",");
	$.post('delOrg.action', {
		'orgIds' : orgIds
	}, function(data) {
		var ptId = curNode.parentTId;
		orgTree.removeNode(curNode);
		curNode = orgTree.getNodeByParam("orgId", curNode.parentId);
		orgTree.selectNode(curNode, false, false);
		showData(curNode);
	});
}
//
function showOrgDetails(orgId) {
	$.post('getOrgDetailsById.action', {
		'orgId' : orgId
	}, function(data) {
		showData(data);
	});
}
function showData(data) {
	$('#menuInfoWell').hide();
	$('#orgShow').show();
	$('#pdisName1').val('');
	$('#pdisName1').hide();
	for (col in data) {
		if ('orgType' == col) {
			if ('1' == data[col]) {
				$('#orgType1').html('单位');
			} else if ('2' == data[col]) {
				$('#orgType1').html('部门');
			}
		} else if ('orgKind' == col) {
			$('#parentOrgType').val(data[col]);
			if ('1' == data[col]) {
				$('#orgKind1').html('联通');
			} else if ('2' == data[col]) {
				$('#orgKind1').html('公安');
			}
		} else if ('orgStatus' == col) {
			if ('1' == data[col]) {
				$('#orgStatus1').html('有效');
			} else if ('0' == data[col]) {
				$('#orgStatus1').html('无效');
			}
		} else if ('orgCode' == col) {
			$('#orgCode1').html(data[col]);
		} else if ('orgName' == col) {
			$('#orgName1').html(data[col]);
		} else if ('disName' == col) {
			$('#disName1').show();
			$('#disName1').html(data[col]);
		} else if ('pdisName' == col) {
			if (data[col]) {
				$('#pdisName1').show();
				$('#pdisName1').html(data[col]);
			} else {
				$('#pdisName1').hide();
			}
		}
	}
}
// 修改页面
function showUpdate() {
	if (!curNode) {
		alertModel('请选择需修改节点');
		return;
	}
	$('#parentDisId').empty();
	$('#disId').empty();
	var parentId = "";
	var childId = "";
	$.post('getOrgDetailsById.action', {
		'orgId' : curNode.orgId
	}, function(data) {
		for (col in data) {
			$('#orgObj [name=' + col + ']').val(data[col]);
		}
		parentId = data.pdisId;
		childId  = data.disId;
	});
	$.post("getFirstDis.action", {}, function(data) {
		if (data) {
			$('#parentDisId').append(
					"<option value=\"\">" + "-------请选择-------" + "</option>");
			for (var i = 0; i < data.length; i++) {
				$('#parentDisId').append(
						"<option value=\"" + data[i].disId + "\" onclick=getSecondDis("+ data[i].disId+",null)>"
								+ data[i].disName + "</option>");
				if(""== parentId || null == parentId){
					document.getElementById("parentDisId").value =childId;
					getSecondDis(childId,null);
				}else{
					document.getElementById("parentDisId").value =parentId;
					getSecondDis(parentId,childId);
				}
			}
		}
	});
//	$.post("getFirstDis.action", {}, function(data1) {
//		if (data1) {
//			$('#parentDisId').append(
//					"<option value=\"\">" + "-------请选择-------" + "</option>");
//			for (var i = 0; i < data1.length; i++) {
//				$('#parentDisId').append(
//						"<option value=\"" + data1[i].disId + "\">"
//								+ data1[i].disName + "</option>");
//				document.getElementById("parentDisId").value = data1[i].disId;
//				$("#parentDisId").val(data1[i].disId);
//				$("#parentDisId").change(childId);
//			}
//		}
//	});

	$('#orgObj input').val("");
	$('#orgShow').hide();
	$('#menuInfoWell').show();
	$('#addOrg').hide();
	$('#updateOrg').show();
	var orgKind = $('#parentOrgType').val();
	$('#orgKind').val(orgKind);
	if (1 == orgKind) {
		$('#orgKindShow').html("联通");
	} else {
		$('#orgKindShow').html("公安");
	}
	var parentId = curNode.parentId;
	if (null == parentId) {
		$('#parentName').html('');
	} else {
		var pNode = orgTree.getNodesByParam("orgId", parentId);
		if (null != pNode[0].orgName) {
			$('#parentName').html(pNode[0].orgName);
		} else {
			$('#parentName').html('');
		}
	}
	$('#updateOrg').html('修改');
}
// 修改组织信息
function updateOrg() {
	$.post("updateOrgById.action", $('#orgObj').serialize(), function(data) {
		alertModel("修改成功！");
		initFrame();
		showData(data);
		resetValidator();
		//将修改节点更新到ztree上
		var treeObj=$.fn.zTree.getZTreeObj("tree");
		var nodes = treeObj.getNodesByParam("orgId",data.orgId);
		nodes[0].orgName=data.orgName;
		treeObj.updateNode(nodes[0]);
	});
}
function showStaff() {
	$('#orgStaffContainer').show();
	$('#menuInfoWell').hide();
	$('#orgShow').hide();
	$('#orgHeader').hide();
	$(".ma_tip").text("组织" + "\"" + curNode.orgName + "\"" + " 下的人员");
	$("#orgStaffTable").DataTable().destroy();
	$("#orgStaffTable")
			.DataTable(
					{
						"ordering" : false,// 排序
						"serverSide" : true,// 开启服务器模式
						"scrollX" : true,// 横向滚动
						ajax : {
							"type" : "POST",
							"url" : 'getOrgStaff.action',// 请求路径
							"contentType" : 'application/x-www-form-urlencoded; charset=UTF-8',
							"dataType" : 'json',
							"data" : {
								"orgId" : curNode.orgId
							}
						},
						columns : [// 对应列
								{
									"data" : null,
									visible : false
								},
								{
									"data" : "staffName",
									className : "text-center",
									render : function(a, b, c, d) {
										return "<a href=\"javascript:staffDetails('"
												+ c.staffId
												+ "')\">"
												+ a
												+ "</a>";
									}
								}, {
									"data" : "loginName",
									className : "text-center"
								}, {
									"data" : "orgName",
									className : "text-center",
									render : function(a, b, c, d) {
										if ('T' == c.staffOrgtype) {
											return c.orgName + '(兼职)'
										} else if ('F' == c.staffOrgtype) {
											return c.orgName + '(主岗)'
										} else if ('J' == c.staffOrgtype) {
											return c.orgName + '(借调)'
										}
									}
								}, {
									"data" : "sex",
									className : "text-center",
									render : function(a, b, c, d) {
										if ('M' == c.sex) {
											return '男';
										} else {
											return '女';
										}
									}
								}, {
									"data" : "mobilPhone",
									className : "text-center"
								}, {
									"data" : "email",
									className : "text-center"
								}, {
									"data" : "phone",
									className : "text-center"
								}, {
									"data" : "staffStatus",
									className : "text-center",
									render : function(a, b, c, d) {
										if ('1' == c.staffStatus) {
											return '有效';
										} else {
											return '无效';
										}
									}
								} ],
						"columnDefs" : [ {// 所有列默认值
							"targets" : "_all",
							"defaultContent" : ''
						} ],
						lengthMenu : [ menuLength, menuLength ],
						"dom" : 'rt<"pull-left mt5"l><"pull-left mt5"i><"pull-right mt5"p><"clear">' // 生成样式
					});
}
function backToMain() {
	$('#orgStaffContainer').hide();
	$('#menuInfoWell').hide();
	$('#orgShow').show();
	$('#orgHeader').show();
}
